package com.obas.access.business.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ease.platform.access.service.impl.AbstractNamespaceAccessServiceImpl;
import com.obas.access.business.service.TestPlanAccessService;
import com.obas.access.business.service.TestPlanItemAccessService;
import com.obas.access.business.service.TestTaskAccessService;
import com.obas.access.business.service.TestTaskItemAccessService;
import com.obas.model.business.TestPlan;
import com.obas.model.business.TestPlanItem;
import com.obas.model.business.TestTask;
import com.obas.model.business.TestTaskItem;
import com.obas.model.business.criteria.TestTaskCriteria;

public class TestTaskAccessServiceImpl extends AbstractNamespaceAccessServiceImpl<TestTask> implements TestTaskAccessService {
    private TestPlanAccessService testPlanAccessService;

    private TestPlanItemAccessService testPlanItemAccessService;

    private TestTaskItemAccessService testTaskItemAccessService;

    public List<TestTask> findByTestPlanId(long testPlanId) {
        TestTaskCriteria parameter = new TestTaskCriteria();
        parameter.setTestPlanId(testPlanId);
        return this.find(parameter);
    }

    @Override
    public TestTask createTaskByTestPlanId(long testPlanId) {
        TestTask task = new TestTask();
        TestPlan plan = testPlanAccessService.findById(testPlanId);
        task.setName(new Date().toString());
        task.setTestPlan(plan);
        task.setTestPlanId(testPlanId);
        task = this.create(task);
        List<TestPlanItem> planItems = testPlanItemAccessService.findByTestPlanId(testPlanId);
        if (planItems != null && planItems.size() > 0) {
            List<TestTaskItem> items = new ArrayList<TestTaskItem>(planItems.size());
            for (TestPlanItem testPlanItem : planItems) {
                TestTaskItem testItem = new TestTaskItem();
                testItem.setTestPlanItem(testPlanItem);
                testItem.setName("taskItem");
                testItem.setTestPlanItemId(testPlanItem.getId());
                testItem.setTestResult(true);
                testItem.setTestTask(task);
                testItem.setTestTaskId(task.getId());
                items.add(testItem);
            }
            testTaskItemAccessService.saveList(items);
        }
        return task;
    }

    public TestPlanAccessService getTestPlanAccessService() {
        return testPlanAccessService;
    }

    public void setTestPlanAccessService(TestPlanAccessService testPlanAccessService) {
        this.testPlanAccessService = testPlanAccessService;
    }

    public TestPlanItemAccessService getTestPlanItemAccessService() {
        return testPlanItemAccessService;
    }

    public void setTestPlanItemAccessService(
            TestPlanItemAccessService testPlanItemAccessService) {
        this.testPlanItemAccessService = testPlanItemAccessService;
    }

    public TestTaskItemAccessService getTestTaskItemAccessService() {
        return testTaskItemAccessService;
    }

    public void setTestTaskItemAccessService(
            TestTaskItemAccessService testTaskItemAccessService) {
        this.testTaskItemAccessService = testTaskItemAccessService;
    }
}
